package client.view;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: sam
 * Date: 16/02/12
 * Time: 06:30
 * To change this template use File | Settings | File Templates.
 */
public class MFWindow extends JFrame {

    public static String MEDIA = "client/media/";

    public MFWindow() {
        super();

         setTitle("Music-alms");
         setSize(600, 370);
         setResizable(false);

         MainPanel mainPanel = new MainPanel();
         mainPanel.init();

         getContentPane().add(mainPanel);

         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

         int x = ( int ) ( dim.getWidth() / 2 ) - ( int ) ( this.getWidth() / 2 );
         int y = ( int ) ( dim.getHeight() / 2 ) - ( int ) ( this.getHeight() / 2 );

         try {
             URL resourceURL = getClass().getClassLoader().getResource(MFWindow.MEDIA+"music_listen.png");
             setIconImage(Toolkit.getDefaultToolkit().getImage(resourceURL));
         } catch (Exception e) {
               ReportsPanel.log("Error: "+e);
         }

        setLocation(x, y);
        setVisible(true);
    }

    class MainPanel extends JPanel {

        JTabbedPane tabbedPane;

        public MainPanel() {

            //ReportsPanel.log(getContentPane());
        }

        public void init() {

            setLayout(new GridLayout(1, 1));
            tabbedPane = new JTabbedPane( JTabbedPane.TOP );
            tabbedPane.setTabLayoutPolicy( JTabbedPane.WRAP_TAB_LAYOUT );
            Font font = new Font("Dialog", Font.BOLD, 11);
            tabbedPane.setFont(font);
            ImageIcon icon;

            DownloadPanel downloadPanel = new DownloadPanel();
            UploadPanel uploadPanel = new UploadPanel();
            SettingsPanel settingsPanel = new SettingsPanel();
            RepositoriesPanel repositoriesPanel = new RepositoriesPanel(downloadPanel.downloadControlPanel, settingsPanel, tabbedPane);
            HelpPanel helpPanel = new HelpPanel();
            ReportsPanel reportsPanel = new ReportsPanel();

            icon = new ImageIcon(getClass().getClassLoader().getResource(MFWindow.MEDIA+"down_arrow.png"));
            tabbedPane.addTab("Download", icon, downloadPanel, "Search & Download Music");

            icon = new ImageIcon(getClass().getClassLoader().getResource(MFWindow.MEDIA+"up_arrow.png"));
            tabbedPane.addTab("Upload", icon, uploadPanel, "Upload Your Own Music");

            icon = new ImageIcon(getClass().getClassLoader().getResource(MFWindow.MEDIA+"database_icon.png"));
            tabbedPane.addTab("Repositories", icon, repositoriesPanel, "Sign in to repository");

            icon = new ImageIcon(getClass().getClassLoader().getResource(MFWindow.MEDIA+"settings_icon.png"));
            tabbedPane.addTab("Settings", icon, settingsPanel, "Settings for Music Finder");

            icon = new ImageIcon(getClass().getClassLoader().getResource(MFWindow.MEDIA+"help_icon.png"));
            tabbedPane.addTab("Help", icon, helpPanel, "Need help using Musicalms?");

            icon = new ImageIcon(getClass().getClassLoader().getResource(MFWindow.MEDIA+"log_icon.png"));
            tabbedPane.addTab("Reports", icon, reportsPanel, "Report a bug.");

            downloadPanel.init(uploadPanel);    // what if downloadPanel isn't ready? Hasn't had child init()'s called yet.

            tabbedPane.setSelectedIndex(2);

            add(tabbedPane);

            // first server call
            repositoriesPanel.init();
        }
    }
}



/**
 The swing top-level containers all have a single root component (JRootPane, see here) which holds several,
 layered sub-containers (content pane, layered pane, glass pane). getContentPane(): frames are wht make up windows in your UI
 and these frames are made up from an object which has 3 layers to it which are called: rootpane, contentpane and glasspane
 from lowest to highest. Generally everything you see in your ui goes into the contentpane. This pane is just a container object
 which holds everything and this is what your getting from this method. Here you can have the following programming format for getContentPane:

 public void add(Component component, Object constraints)
 {
     getContentPane().add(component, constraints);
 }

**/